package com.police.db;

/**
 * Created by LEO on 13-6-6.
 */

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class EventManager {
    private DBHelper helper;
    private SQLiteDatabase db;

//    public EventManager(Context context) {
//        helper = new DBHelper(context);
//        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
//        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
//        db = helper.getWritableDatabase();
//    }
    
    
    public EventManager(DBHelper helper) {
        this.helper = helper;
        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);
        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
        db = helper.getWritableDatabase();
    }
    

    /**
     * add Events
     *
     * @param Events
     */
    public void add(List<Event> Events) {
        db.beginTransaction();    //开始事务
        try {
            for (Event Event : Events) {
//                db.execSQL("INSERT INTO Event VALUES(null, ?, ?, ?)", new Object[]{Event.name, Event.time, Event.userName,Event.phoneNum});
                ContentValues cv=new ContentValues();    //实例化ContentValues对象
                cv.put("adminName", Event.adminName);
                cv.put("name", Event.name);                  //插入字段值
                cv.put("time", Event.time); 
                cv.put("userName", Event.userName);//插入字段值
                cv.put("phoneNum", Event.phoneNum);
                db.insert("Event", null, cv);         //执行insert()方法
            }
            
            db.setTransactionSuccessful();    //设置事务成功完成
        } catch (Exception e) {
			// TODO: handle exception
		} finally {
            db.endTransaction();    //结束事务
        }
    }
    /**
     *
     * del event
     * @param eventName
     */
    public void del(String eventName){
        db.delete("Event", "name = ?", new String[]{eventName});
    }
    /**
     * query all Events, return list
     * @return List<Event>
     * 
     * 查询所有事件（但只返回，查找当前某个用户的事件）
     * 
     */
    public List<Event> query(String whichUser) {
        ArrayList<Event> Events = new ArrayList<Event>();
        Cursor c = queryTheCursor();
        while (c.moveToNext()) {
            Event Event = new Event();
            Event._id = c.getInt(c.getColumnIndex("_id"));
            Event.adminName = c.getString(c.getColumnIndex("adminName"));
            Event.name = c.getString(c.getColumnIndex("name"));
            Event.time = c.getString(c.getColumnIndex("time"));
            Event.userName = c.getString(c.getColumnIndex("userName"));
            Event.phoneNum = c.getString(c.getColumnIndex("phoneNum"));
            /**
             * 根据查找某个用户的名字，来取出该用户所办理的事件
             */
            if (Event.adminName.equals(whichUser)) {
            	Events.add(Event);
            }
        }
        c.close();
        return Events;
    }

    /**
     * 查询当前登录用户（非管理员）的事件
     * @return
     */
    public List<Event> queryPresentUserEvent(String adminUserName) {
        ArrayList<Event> Events = new ArrayList<Event>();
        Cursor c = queryTheCursor();
        while (c.moveToNext()) {
            Event Event = new Event();
            Event._id = c.getInt(c.getColumnIndex("_id"));
            Event.adminName = c.getString(c.getColumnIndex("adminName"));
            Event.name = c.getString(c.getColumnIndex("name"));
            Event.time = c.getString(c.getColumnIndex("time"));
            Event.userName = c.getString(c.getColumnIndex("userName"));
            Event.phoneNum = c.getString(c.getColumnIndex("phoneNum"));
            if (Event.adminName.equals(adminUserName)) {
            	Events.add(Event);
            }
        }
        c.close();
        return Events;
    }
    
    
    /**
     * query all Events, return cursor
     *
     * @return Cursor
     */
    public Cursor queryTheCursor() {
        Cursor c = db.rawQuery("SELECT * FROM Event", null);
        return c;
    }

    /**
     * close database
     */
    public void closeDB() {
        db.close();
    }
}

